package org.water.sys.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.water.base.service.BaseService;
import org.water.base.support.BeanQuery;
import org.water.common.param.ConstParams;
import org.water.sys.entity.SysOnlineUser;
import org.water.sys.entity.SysUser;
import org.water.sys.repository.RoleDao;
/**
 * 用户 service
 * @author qzy
 *
 */
import org.water.sys.repository.UserDao;
@Service
public class UserService extends BaseService<SysUser, String> {

	public UserService() {
		// TODO Auto-generated constructor stub
	}
    /**
     * 用户dao
     * @return
     */
	protected UserDao getUserRepository()
	{
		return (UserDao) super.baseRespository;
	}
	@Autowired
	protected RoleDao roleDao;
	@Autowired
	private SessionDAO sessionDAO;
	/**
	 * 模糊查询用户
	 * @param userName
	 * @return
	 */
	public Page<SysUser> findByUserNameLike(String userName,PageRequest pageRequest)
	{
		Page<SysUser> users=null;
		users=getUserRepository().findByUserNameLikeAndDelStatus(userName,0,pageRequest);
		
		return users;
	}
	/**
	 * 模糊查询用户
	 * @param userName
	 * @return
	 */
	public Page<SysUser> findByUserNameLike(String userName,String aliasName,PageRequest pageRequest)
	{
		Page<SysUser> users=null;
		//users=getUserRepository().findByUserNameLikeOrAliasNameLikeAndDelStatus(userName,0,pageRequest);
		
		BeanQuery<SysUser> beanQuery=this.baseRespository.createBeanQuery(SysUser.class);
		String[] ps={"userName","aliasName"};
		users=beanQuery.orLike(ps, userName).eq("delStatus", 0).orderDesc("createDate")
				.page(pageRequest.getPageNumber(), pageRequest.getPageSize());
		
		return users;
	}
	/**
	 * 模糊查询用户
//	 * @param userName
	 * @return
	 */
	public List<SysUser> findByUserNameLike(String userName)
	{
		List<SysUser> users=null;
		users=getUserRepository().findByUserNameLike2(userName);
		return users;
	}
	/**
	 * 更新用户别名
	 * @param id
	 * @param aliasName
	 * @return
	 */
	public int updateUserAliasName(String id,String aliasName)
	{
		int result=-1;
		result=getUserRepository().updateUserAliasName(id, aliasName);
		return result;
	}
	/**
	 * 根据用户名与密码获取用户
	 * @param userName
	 * @param password
	 * @return
	 */
	public SysUser findByUserNameAndPassword(String userName,String password)
	{
		SysUser user=getUserRepository().findByUserNameAndPassword(userName, password);
		
		return user;
	}
	/**
	 * 根据用户名获取用户
	 * @param userName
	 * @return
	 */
	public SysUser findUserByUserName(String userName)
	{
		BeanQuery<SysUser> beanQuery=getUserRepository().createBeanQuery(SysUser.class);
		beanQuery.eq("userName", userName);
		beanQuery.eq("delStatus", 0);
		//getUserRepository().findByUserName(userName);
		return beanQuery.one();
	}
	
	
	//用户管理
	/**
	 * 保存用户
	 * @param user
	 */
	public void save(SysUser user)
	{
		getUserRepository().saveAndFlush(user);
	}
	/**获取在线用户
	 * 
	 * @return
	 */
	public List<SysOnlineUser> findOnlineUser()
	{
		List<SysOnlineUser> users=new ArrayList<>();
		Collection<Session> sessions = sessionDAO.getActiveSessions();
		for(Session session:sessions){
			SysOnlineUser user=new SysOnlineUser();
			SysUser sysUser=(SysUser) session.getAttribute(ConstParams.CURRENT_USER);
			if(sysUser!=null)
			{
				users.add(user);
				user.setId(sysUser.getId());
				user.setUserName(sysUser.getUserName());
				user.setAliasName(sysUser.getAliasName());
				user.setIp(session.getHost());
				user.setStartDate(session.getStartTimestamp());
				user.setLastDate(session.getLastAccessTime());
				user.setTimeOut(session.getTimeout()/(1000*60));
				user.setSessionId(String.valueOf(session.getId()));
			}
		}
		return  users;
	}
}
